import React from 'react';
import { Image, NativeModules, Platform, ScrollView, StyleSheet, Text, View, TouchableOpacity, DeviceEventEmitter, TextInput } from 'react-native';

import { AlertDialog, SimpleItemsBottom, FilterDialog } from 'react-native-pickers'

import ScreenUtils from '../Utils/ScreenUtils'
import ColorUtils from '../Utils/ColorUtils'
import RouterUtils from '../Utils/RouterUtils'
import ToastUtils from '../Utils/ToastUtils'
import PushUtil from '../Umeng/PushUtil'
import StorageUtils from '../Utils/StorageUtils'
import Constant from "../Constant/Constant"
import HttpUtils from "../Utils/HttpUtils"
import RuleUtils from "../Utils/RuleUtils";

export default class PageSearchList extends React.Component
{
    static navigationOptions = ({navigation}) => ({
        gesturesEnabled: false,//是否支持滑动返回收拾，iOS默认支持，安卓默认关闭
        headerTitle: '搜索',
        headerStyle: {
            backgroundColor: ColorUtils.blue,
            height: ScreenUtils.statusBarHeight,
        },
        headerRight: (<Text></Text>),
        headerTintColor: ColorUtils.white,
        headerTitleStyle: {
            fontSize: ScreenUtils.Font_18,
            color: ColorUtils.white,
            fontWeight: 'normal',
            flex: 1,
            alignSelf: 'center',
            textAlign: 'center',
        }
    });

    constructor(props)
    {
        super(props);
        this.state = {
            searchText: '',
            searchCases: [],
            isShowCancelDelete: false,
        };
    }

    componentDidMount()
    {
        RouterUtils.pushRouter('PageSearchList', this.props.navigation.state.key);
        this.searchCase();
    }

    componentWillUnmount()
    {
        RouterUtils.popRouter('PageSearchList', this.props.navigation.state.key);
    }

    searchCase()
    {
        try
        {
            var that = this;
            StorageUtils.get('session').then((session) =>
            {
                if (session != '' && session != null)
                {
                    HttpUtils.getJson(Constant.searchCase + '?caseNo=' + that.state.searchText, session, function (resultcode, data)
                    {
                        console.log('data:' + JSON.stringify(data.records));
                        that.setState({
                            searchCases: data.records
                        })
                    }, function (resultcode, reason)
                    {
                        ToastUtils.toastShortBottom('错误码:' + resultcode + ',错误原因:' + reason);
                    });
                }
            });
        }
        catch (error)
        {
            ToastUtils.toastShortBottom(error);
        }
    }

    jumpDetail(status, id, processInstanceId)
    {
        this.props.navigation.navigate('PageLsajDetail', {'title': '历史案件', 'status': status, 'id': id, 'processInstanceId': processInstanceId});
    }

    searchChange(text)
    {
        if (text.length > 0)
        {
            this.setState({
                isShowCancelDelete: true,
                searchText: text
            })
        } else
        {
            this.setState({
                isShowCancelDelete: false,
                searchText: text
            })
            this.searchCase();
        }
    }

    searchEnd()
    {
        this.searchCase();
    }

    cancelSearch()
    {
        this.setState({
            searchText: '',
            isShowCancelDelete: false
        })
        this.searchCase();
    }

    renderSearchCase()
    {
        let allData = [];
        for (let i = 0; i < this.state.searchCases.length; i++)
        {
            let item = this.state.searchCases[i];
            allData.push(
                <TouchableOpacity key={item.id} activeOpacity={0.8} onPress={() => this.jumpDetail('未上报', item.id, item.processInstanceId)} style={{paddingTop: 16, paddingLeft: 16, paddingRight: 16}}>
                    <View style={{flexDirection: 'row'}}>
                        <View style={{height: '100%', backgroundColor: ColorUtils.blue, width: 3}}/>
                        <View style={{marginLeft: 4, flex: 1, flexDirection: 'column', justifyContent: 'center', marginLeft: 8}}>
                            <View style={{flexDirection: 'row', alignItems: 'center'}}>
                                <Text style={{fontSize: ScreenUtils.Font_14, color: ColorUtils.light_gray}}>案件编号:</Text>
                                <Text style={{marginLeft: 8, fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>{item.caseNo == "" ? '无' : item.caseNo}</Text>
                            </View>
                            <View style={{marginTop: 8, flexDirection: 'row', alignItems: 'center'}}>
                                <Text style={{fontSize: ScreenUtils.Font_14, color: ColorUtils.light_gray}}>案件类型:</Text>
                                <Text style={{marginLeft: 8, fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>{item.categoryName}</Text>
                            </View>
                            <View style={{marginTop: 8, flexDirection: 'row', alignItems: 'center'}}>
                                <Text style={{fontSize: ScreenUtils.Font_14, color: ColorUtils.light_gray}}>案件大类:</Text>
                                <Text style={{marginLeft: 8, fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>{item.categoryOneName}</Text>
                            </View>
                            <View style={{marginTop: 8, flexDirection: 'row', alignItems: 'center'}}>
                                <Text style={{fontSize: ScreenUtils.Font_14, color: ColorUtils.light_gray}}>案件小类:</Text>
                                <Text style={{marginLeft: 8, fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>{item.categoryTwoName}</Text>
                            </View>
                        </View>
                        <View style={{height: 26, justifyContent: 'center', padding: 6, borderRadius: 4, backgroundColor: '#FFD1D5'}}>
                            <Text style={{color: '#FF192F', fontSize: ScreenUtils.Font_10}}>
                                {RuleUtils.converState(item.state)}
                            </Text>
                        </View>
                    </View>
                    <View style={{marginTop: 16, height: 1, borderColor: ColorUtils.line, borderBottomWidth: 1}}/>
                </TouchableOpacity>
            );
        }
        return allData;
    }

    render()
    {
        return (
            <View style={styles.container}>
                <View style={{margin: 32, marginTop: 16, marginBottom: 8, paddingLeft: 16, paddingRight: 16, backgroundColor: '#DDDDDD', borderRadius: 32, flexDirection: 'row', alignItems: 'center'}}>
                    <Image resizeMode={Image.resizeMode.stretch} source={require('../img/search_icon.png')} style={{width: 20, height: 20}}/>
                    <TextInput value={this.state.searchText} underlineColorAndroid='transparent' keyboardType='numeric' maxLength={30} placeholder='请输入搜索单号' onChangeText={(text) => this.searchChange(text)} onSubmitEditing={() => this.searchEnd()}
                               multiline={false} style={{marginLeft: 8, fontSize: ScreenUtils.Font_14, flex: 1, height: 40, color: ColorUtils.black}}/>
                    <TouchableOpacity activeOpacity={0.8} onPress={() => this.cancelSearch()}>
                        <Image resizeMode={Image.resizeMode.stretch} source={require('../img/cancel_search.png')} style={{width: 22, height: 22, display: this.state.isShowCancelDelete ? 'flex' : 'none',}}/>
                    </TouchableOpacity>
                </View>
                {this.renderSearchCase()}
                <FilterDialog
                    itemKey='value'
                    ref={ref => this.SimpleItemsDialog = ref}
                    onSubmit={(which) =>
                    {
                        alert(which)
                    }}/>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: ColorUtils.white,
    },
});


